﻿'Importação dos NameSpaces
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports BLL
Imports DTO
Public Class frmLogin
    'Controle de tentativas de acesso
#Region ">> Instancias              <<"
    Dim bllLoginUsuario As New BLLLoginUsuarios
    Dim dtoLoginUsuario As New DTOLoginUsuarios
#End Region
#Region ">> Variaveis               <<"
    Dim Tentativas As Short = 0
#End Region
#Region ">> Rotinas/Funções do Form <<"
    Private Sub HabilitarControle(ByVal Status As Boolean)
        '----------------------------------
        'Habilitar/Desabilitar os controles
        '----------------------------------
        If Status = False Then
            lblConexao.Visible = True
        End If
        txtLogin.Enabled = Status
        txtSenha.Enabled = Status
        cmdOK.Enabled = Status
        cmdCancelar.Enabled = Status
    End Sub



#End Region
#Region ">> Ações                   <<"
    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        'Cancelar
        Me.Close()
    End Sub
    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        'Validar campo login
        If txtLogin.Text = Nothing Then
            lblConexao.ForeColor = Color.Gold
            lblConexao.Text = "Informe o login para entrar."
            txtLogin.Focus()
            'Validar campo senha
            Exit Sub
        ElseIf txtSenha.Text = Nothing Then
            lblConexao.ForeColor = Color.Gold
            lblConexao.Text = "Informe a senha para entrar."
            txtSenha.Focus()
            Exit Sub
        End If
        HabilitarControle(False)
        dtoLoginUsuario.Usuario = txtLogin.Text.Trim
        dtoLoginUsuario.Senha = txtSenha.Text.Trim
        'Chamar a função de validação
        lblConexao.Text = "Aguarde o processamento..."
        If bllLoginUsuario.EntrarUsuario(dtoLoginUsuario) = False Then
            Tentativas += 1
            'Verificação de tentativas
            If Tentativas <= 3 Then
                lblConexao.ForeColor = Color.Red
                lblConexao.Text = "Usuário/Senha inválido. Tente novamente."
                txtLogin.Focus()
                HabilitarControle(True)
            Else
                MessageBox.Show("Acesso não permitido. Contate o SUPERVISOR do sistema.", "Restaurante .NET", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End
            End If
        Else
            'Acesso permitido
            Dim frm As New frmPrincipal
            frm.ToolStripStatusLabelUsuarioLogado.Text = txtLogin.Text
            lblConexao.ForeColor = Color.Black
            frm.Show()
            Me.Hide()
        End If
    End Sub
    Private Sub txtLogin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLogin.KeyPress
        'Se o usuário pressionar ENTER
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            txtSenha.Focus()
        End If
    End Sub
    Private Sub txtSenha_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSenha.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            cmdOK.Focus()
        End If
    End Sub
#End Region

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
